|
|
|
|
|
|
|
Using ActiveX Data Objects |
|
|
|
|
|
|
|
|
Working with the ADO Data control is useful to a point, but the real power comes into play when you use ADO objects in your Visual Basic code. The objects that make up the ADO object model are listed in Table 28.1. |
|
|
|
|
| Table 28.1 ActiveX Data Objects | | Object | Description | | Recordset | Contains the records that make up the results of a query | | Connection | Allows control over the connection to the data source | | Command | Executes database commands and queries with parameterized queries | | Error | Retrieves errors from ADO | | Field | Represents a piece of data in a recordset | | Parameter | Works with the Command object to set up a parameter in a query or stored procedure | | Property | Allows you access to ADO object properties |
|
|
|
|
|
|
To use these ADO objects, you must add a reference to your project. To do so, choose Project, References. Place a check mark next to Microsoft ActiveX Data Objects 2.0 library and click the OK button. After referencing the object library, you can use the following prefixes to declare ADO objects: |
|
|
|
|
|
|
|
|
ADODBThis object library contains all the objects listed in Table 28.1. |
|
|
|
|
|
|
|
|
ADORThis object library contains only the objects associated with a recordset (RecordSet, Field, Property). ADOR is included with IE 4.01, allowing developers to pass disconnected recordsets across the Internet. |
|
|
|
|
|
|
|
|
In the following sections, you'll explore some of the properties, methods, and events of the ADO objects. A complete list of these elements is available in the Visual Basic Object Browser, which you can display by pressing F2. |
|
|
|
|
|
|
|
|
Making the Connection with ADO |
|
|
|
|
|
|
|
|
The Connection object is used to establish a connection to a data source. Its most important property is the ConnectionString property, which contains the information used to connect to a database. You control the state of the database connection with the Open and Close methods. |
|
|
|
|
|
|
|
|
Opening and Closing a Connection |
|
|
|
|
|
|
|
|
The first step in opening a connection is to create a new instance of the ADODB.Connection object, as follows: |
|
|
|
|
|
|
|
|
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection |
|
|
|
|
|